/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.listeners
{
	import funit.core.ITestListener;
	import funit.core.TestName;
	import funit.core.TestOutput;
	import funit.core.TestResult;
	
	import mx.collections.ArrayCollection;
	
	public class TestListenerCollection implements ITestListener
	{
		protected var _listeners:ArrayCollection = new ArrayCollection();
		
		
		/**
		 * 
		 */
		public function TestListenerCollection()
		{
			
		}
		
		
		/**
		 * @param listener
		 */
		public function addListener( listener:ITestListener ) : void
		{
			if (!_listeners.contains(listener))
			{
				_listeners.addItem(listener);
			}
		}
		
		/**
		 * @param listener
		 * @return 
		 */
		public function removeListener( listener:ITestListener ) : ITestListener
		{
			var index:int = _listeners.getItemIndex(listener);
			
			if (index != -1)
			{
				return _listeners.removeItemAt(index) as ITestListener;
			}
			else
			{
				return null;
			}
		}
		
		/**
		 * @param testName
		 * @param testCount
		 */
		public function runStarted( testName:String, testCount:int ) : void
		{
			for each ( var listener:ITestListener in _listeners )
			{
				listener.runStarted(testName, testCount);
			}
		}
		
		/**
		 * @param result
		 */
		public function runFinished( result:TestResult ) : void
		{
			for each ( var listener:ITestListener in _listeners )
			{
				listener.runFinished(result);
			}
		}
		
		/**
		 * @param error
		 */
		public function runFinishedError( error:Error ) : void
		{
			for each ( var listener:ITestListener in _listeners )
			{
				listener.runFinishedError(error);
			}
		}
		
		/**
		 * @param testName
		 */
		public function testStarted( testName:TestName ) : void
		{
			for each ( var listener:ITestListener in _listeners )
			{
				listener.testStarted(testName);
			}
		}
		
		/**
		 * @param result
		 */
		public function testFinished( result:TestResult ) : void
		{
			for each ( var listener:ITestListener in _listeners )
			{
				listener.testFinished(result);
			}
		}
		
		/**
		 * @param testName
		 */
		public function suiteStarted( testName:TestName ) : void
		{
			for each ( var listener:ITestListener in _listeners )
			{
				listener.suiteStarted(testName);
			}
		}
		
		/**
		 * @param result
		 */
		public function suiteFinished( result:TestResult ) : void
		{
			for each ( var listener:ITestListener in _listeners )
			{
				listener.suiteFinished(result);
			}
		}
		
		/**
		 * @param error
		 */
		public function unhandledError( error:Error ) : void
		{
			for each ( var listener:ITestListener in _listeners )
			{
				listener.unhandledError(error);
			}
		}
		
		/**
		 * @param testOutput
		 */
		public function testOutput( testOutput:TestOutput ) : void
		{
			for each ( var listener:ITestListener in _listeners )
			{
				listener.testOutput(testOutput);
			}
		}
		
	}
	
}
